<?php
App::uses('AppModel', 'Model');
/**
 * Post Model
 *
 */
class Post extends AppModel {
	#parameter type
	const rtype = 1; 
	const topic = 2;

	#post type
	const news = 1;
	const travel = 2;
	const consult = 3;

	#consultant type
	const consult1 = 1; #Mua ve may bay
	const consult2 = 2; #Qui dinh hang khong
	const consult3 = 3; #Truoc chuyen bay
	const consult4 = 4; #Tren chuyen bay

	var $actsAs = array(
		'MeioUpload.MeioUpload' => array(
			'image' => array(        
				'thumbnails' => true ,
				'allowed_mime' => array('image/jpeg', 'image/pjpeg', 'image/gif', 'image/png'),
				'allowed_ext' => array('.jpg', '.jpeg', '.png', '.gif'),
				'thumbsizes' => array(
				  'small'  => array('height'=>120, 'width'=>260, 'forceAspectRatio' => 'C', 'thumbnailQuality' => 100, 'zoomCrop' => true), 
				  'medium' => array('width'=>260)
		  ),
		  'default' => 'default.jpg'
		) 
	));


	public $hasMany = array(
		'Comments' => array(
			'className' => 'Comment',
		 )
	);

/**
 * Display field
 *
 * @var string
 */
	public $displayField = 'title';
	public $name = 'Post';

/**
 * Validation rules
 *
 * @var array
 */
	public $validate = array(
		'title' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'description' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'content' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'rtype' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'status' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
	);

	public function beforeSave(){
		$slug = $this->replaceAlpha($this->data["Post"]["title"]);
		$this->data["Post"]["slug"] = Inflector::slug($slug);
		return true;
	}

/**
 * getNews: the newest news
 *
 * @var array
 */
  public function getNews(){
		$news = $this->find('first', 
							array(
										'conditions' => array('rtype' => 1),
										'order' => array('created_at' => 'desc')
									 )
							);
		return $news;
  }

/**
 * getRandTravel: the random travel
 *
 * @var array
 */
	public function getRandTravel(){
		$travel = $this->find('first', 
							array(
									'conditions' => array('rtype' => 2),
									'order' => 'rand()'
									 )
							);
		return $travel;
	}

/**
 * getConsultsByTopic
 *
 * @var array
 */
	public function getConsultsByTopic($topic){
		$post = $this->find('all', array('conditions' => array('rtype' => Post::consult, 'topic' => $topic)));
		return $post;
	}

/**
 * getTravelsForRightPanel: the random travel
 *
 * @var array
 */
	public function getTravelsForRightPanel(){
		$posts = $travel = $this->find('all', 
							array(
									'conditions' => array('rtype' => 2),
									'order' => 'rand()',
									'limit' => 2
									 )
							);
		return $posts;
	}

	static public function getMenuTab($type){
		$str = "";
		if($type == Post::news){
			$str = "news";
		}elseif($type == Post::travel){
			$str = "travel";
		}
		return $str;
	}
}
